package com.heima.common;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PageResult<T> {
    private Integer pageIndex = 1;
    private Integer pageSize = 10;
    private String totalPage;
    private String totalCount;
    private List<T> currentPageRecords;

    /**
     * @param page 源分页对象
     * @return 目标分页数据对象
     */
    public PageResult<T> of(Page<?> page, Class<T> clazz) {
        setTotalCount(StringUtils.fromLong(page.getTotal()));
        setTotalPage(StringUtils.fromLong(page.getPages()));
        setCurrentPageRecords(BeanUtil.copyToList(page.getRecords(), clazz));
        return this;
    }
}
